#李宏毅机器学习
#梯度下降法实战，学习率较低，无法达到目标位置

# 导入工具包
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# 生成数据
xdata = [338.,333.,328.,207.,226.,25.,179.,60.,208.,606.]
ydata = [640.,633.,619.,393.,428.,27.,193.,66.,226.,1591.]
# ydata = b + w * xdata

blst = np.arange(-200, -100, 1)
wlst = np.arange(-5, 5, 0.1)
#TODO: z为blst与wlst各交点处的损失函数（除以长度）


# initial parameters: 偏置，权重，学习率，迭代次数
b, w, eta, iterations = -120, -4, 0.0000001, 100000
# 用于保存b和w的历史数据
bhist, whist = [b], [w]

#TODO: 用梯度下降法计算b和h的取值


# plot the figure
plt.contourf(blst, wlst, z, 50, alpha = 0.5, cmap = plt.get_cmap('jet'))
plt.plot([-188.4], [2.67], 'x', ms = 12, markeredgewidth = 3, color = 'orange')
plt.plot(bhist, whist, 'o-', ms = 3, lw = 1.5, color = 'black')
plt.xlim(-200, -100)
plt.ylim(-5, 5)
plt.xlabel(r'$b$', fontsize = 16)
plt.ylabel(r'$w$', fontsize = 16)
plt.show()


